import logging
import time

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


class BasePageObject:
    def __init__(self, driver):
        self.driver = driver

    # 等待元素可见
    def wait_element_visible(self, loc, path, times=10, poll_frequency=0.5):
        """
        :param poll_frequency:等待元素可见查询时间
        :param times: 等待元素可见最长时间
        :param loc: 元素定位
        :param path: 元素位置 例如：百度首页_搜索框
        :return:
        """
        try:
            start_time = time.time()
            WebDriverWait(self.driver, timeout=times, poll_frequency=poll_frequency).until(EC.visibility_of_element_located(loc))
            end_time = time.time()
            logging.info('等待元素可见耗时，', (end_time-start_time))
        except Exception as e:
            logging.error(e)
            self.save_screen_shot(path)

    # 等待元素存在
    def wait_element_presence(self):
        pass

    # 查找元素
    def find_element(self, loc, path):
        pass

    # 点击元素
    def click_element(self):
        pass

    # 输入信息
    def input_text(self):
        pass

    # 截图
    def save_screen_shot(self,path):
        file_path = path+'/1.png'
        try:
            self.driver.save_screenshot(file_path)
        except Exception as e:
            logging.error(e)
        pass
